﻿/******************************************************    
文件名: GetTerminology.cs
版权  (c)  2010-11-23 淘渺圈圈工作室
作者:黄媛媛
创建日期:2010-12-05
维护人员:黄媛媛
维护日期:2010-12-05
文件功能描述:金属材料热处理网络学习平台_访问Terminology相关数据表操作
******************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Web;
using Mmht.DB;
using System.Configuration;
using System.Collections;


namespace Mmht.BT
{
    public class GetTerminology
    {
        TableTerminologyDB tt = new TableTerminologyDB();
        GetQuestion gq = new GetQuestion();
        int total_choice = 15;//选择题总数目
        int blank_number = 15;//填空题总数目

        private DataTable getAllTerminology()
        {
            return tt.getTerminology();
        }

        public DataTable getQuestionTable()
        {
            DataTable dt = new DataTable();
            dt = this.getAllTerminology();

            DataTable table = new DataTable();
            table.Columns.Add("number");
            table.Columns.Add("id");
            table.Columns.Add("question");
            table.Columns.Add("answer");
            table.Columns.Add("A");
            table.Columns.Add("B");
            table.Columns.Add("C");
            table.Columns.Add("D");



            int max = dt.Rows.Count;
            List<int> list = new List<int>();
            list = gq.getQuestionID(max, total_choice);

            string id = string.Empty;
            string question = string.Empty;

            for (int i = 0; i < list.Count; i++)
            {
                DataRow row = table.NewRow();
                int rowNumber = i + 1;
                row["number"] = rowNumber.ToString();
                //确定中文或英文
                Random rd = new Random();
                int type = rd.Next(1, 3);
                string question_filed = string.Empty;
                string answer_filed = string.Empty;
                if (type == 1)
                {
                    question_filed = "name_cn";
                    answer_filed = "name_en";
                }
                else if (type == 2)
                {
                    question_filed = "name_en";
                    answer_filed = "name_cn";
                }

                int dtid = list[i];
                id = "fanyi_" + dt.Rows[dtid]["id"].ToString();
                //row 添加 id
                row["id"] = id;
                row["question"] = dt.Rows[dtid][question_filed].ToString();

                //错误选项
                List<int> list_wrong_id = new List<int>();
                list_wrong_id.Clear();
                int id_wrong = rd.Next(max);
                ////生成错误选项
                for (int j = 0; j < 3; j++)
                {
                    while (id_wrong == dtid || list_wrong_id.Contains(id_wrong))
                    {
                        id_wrong = rd.Next(max);
                    }
                    list_wrong_id.Add(id_wrong);
                }
                //随机生成正确选项
                int id_right = rd.Next(1, 4);
                //生成选项
                List<string> list_choice = new List<string>();
                list_choice.Clear();
                list_choice.Add("A");
                list_choice.Add("B");
                list_choice.Add("C");
                list_choice.Add("D");
                //移除正确选项
                string answer = string.Empty;
                switch (id_right)
                {
                    case 1: answer = "A"; break;
                    case 2: answer = "B"; break;
                    case 3: answer = "C"; break;
                    case 4: answer = "D"; break;
                    default: break;
                }
                list_choice.Remove(answer);
                //row 中添加入 正确选项
                row["answer"] = answer;
                row[answer] = dt.Rows[dtid][answer_filed].ToString();
                //row 中添加入 错误选项
                for (int j = 0; j < list_choice.Count; j++)
                {
                    row[list_choice[j]] = dt.Rows[list_wrong_id[j]][answer_filed].ToString();
                }

                //添加新题
                table.Rows.Add(row);
            }
            return table;
        }

        //15道填空题
        public DataTable getBlank()
        {
            DataTable dt = new DataTable();
            dt = this.getAllTerminology();

            DataTable table = new DataTable();
            table.Columns.Add("number");
            table.Columns.Add("id");
            table.Columns.Add("question");
            table.Columns.Add("answer");
            table.Columns.Add("answertype");
            
            int max = dt.Rows.Count;
            List<int> list = new List<int>();
            list = gq.getQuestionID(max, blank_number);

            string id = string.Empty;
            string question = string.Empty;
            Random rd = new Random();
            for (int i = 0; i < list.Count; i++)
            {
                DataRow row = table.NewRow();
                int rowNumber = i + 1;
                row["number"] = rowNumber.ToString();
                //确定中文或英文
                int type = rd.Next(1, 3);
                string question_filed = string.Empty;
                string answer_filed = string.Empty;
                string answer_type = string.Empty;
                if (type == 1)
                {
                    question_filed = "name_cn";
                    answer_filed = "name_en";
                    answer_type = "英文翻译";
                }
                else
                {
                    question_filed = "name_en";
                    answer_filed = "name_cn";
                    answer_type = "中文翻译";
                }
                int dtid = list[i];
                id = "blank_" + dt.Rows[dtid]["id"].ToString();
                //row 添加 id
                row["id"] = id;
                row["question"] = dt.Rows[dtid][question_filed].ToString();
                row["answer"] = dt.Rows[dtid][answer_filed].ToString();
                row["answertype"] = answer_type;
                //添加新题
                table.Rows.Add(row);
            }
            return table;
        }

        //15道单选题
        public DataTable getChoice()
        {
            DataTable dt = new DataTable();
            dt = this.getAllTerminology();

            DataTable table = new DataTable();
            table.Columns.Add("number");
            table.Columns.Add("id");
            table.Columns.Add("question");
            table.Columns.Add("answer");
            table.Columns.Add("A");
            table.Columns.Add("B");
            table.Columns.Add("C");
            table.Columns.Add("D");

            int max = dt.Rows.Count;
            List<int> list = new List<int>();
            list = gq.getQuestionID(max, blank_number);

            string id = string.Empty;
            string question = string.Empty;
            Random rd = new Random();
            for (int i = 0; i < list.Count; i++)
            {
                DataRow row = table.NewRow();
                int rowNumber = i + 1;
                row["number"] = rowNumber.ToString();
                //确定中文或英文
                
                //int type = rd.Next(1, 3);
                int type = 1;
                string question_filed = string.Empty;
                string answer_filed = string.Empty;
                if (type == 1)
                {
                    question_filed = "name_cn";
                    answer_filed = "definition_cn";
                }
                else
                {
                    question_filed = "name_en";
                    answer_filed = "definition_en";
                }

                int dtid = list[i];
                id = "choice_" + dt.Rows[dtid]["id"].ToString();
                //row 添加 id
                row["id"] = id;
                row["question"] = dt.Rows[dtid][question_filed].ToString();

                //错误选项
                List<int> list_wrong_id = new List<int>();
                list_wrong_id.Clear();
                int id_wrong = rd.Next(max);
                ////生成错误选项
                for (int j = 0; j < 3; j++)
                {
                    while (id_wrong == dtid || list_wrong_id.Contains(id_wrong))
                    {
                        id_wrong = rd.Next(max);
                    }
                    list_wrong_id.Add(id_wrong);
                }
                //随机生成正确选项
                int id_right = rd.Next(1, 5);
                //生成选项
                List<string> list_choice = new List<string>();
                list_choice.Clear();
                list_choice.Add("A");
                list_choice.Add("B");
                list_choice.Add("C");
                list_choice.Add("D");
                //移除正确选项
                string answer = string.Empty;
                switch (id_right)
                {
                    case 1: answer = "A"; break;
                    case 2: answer = "B"; break;
                    case 3: answer = "C"; break;
                    case 4: answer = "D"; break;
                    default: break;
                }
                list_choice.Remove(answer);
                //row 中添加入 正确选项
                row["answer"] = answer;
                row[answer] = dt.Rows[dtid][answer_filed].ToString();
                //row 中添加入 错误选项
                for (int j = 0; j < list_choice.Count; j++)
                {
                    row[list_choice[j]] = dt.Rows[list_wrong_id[j]][answer_filed].ToString();
                }

                //添加新题
                table.Rows.Add(row);
            }
            return table;
        }
    }
}
